package com.vanvalt.payhui.modules.pay.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.vanvalt.payhui.modules.pay.entity.Banner;
import com.vanvalt.payhui.modules.pay.service.BannerService;

/**
 * @author Xiaoyang
 *
 */
@Controller
@RequestMapping(value = "${adminPath}/pay/banner")
public class BannerController extends BaseController {

	@Autowired
	private BannerService bannerService;
	
	@ModelAttribute
	public Banner get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return bannerService.get(id);
		}else{
			return new Banner();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(Banner banner, HttpServletRequest request, HttpServletResponse response, Model model) {

		String type = banner.getType();
		if(type != null && "0".equals(type)){ // 查询所有类型（type: 0-All 1-PC 2-Mobile）
			banner.setType(null);
		}
        Page<Banner> page = bannerService.findPage(new Page<Banner>(request, response), banner); 
        model.addAttribute("page", page);
		return "modules/pay/bannerList";
	}
	
	@RequestMapping(value = "form")
	public String form(Banner banner, Model model) {
		
		model.addAttribute("banner", banner);
		return "modules/pay/bannerForm";
	}
	
	@RequestMapping(value = "save")
	public String save(Banner banner, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, banner)){
			return form(banner, model);
		}
		bannerService.save(banner);
		addMessage(redirectAttributes, "保存Banner信息'" + StringUtils.abbr(banner.getTitle(),50) + "'成功");
		return "redirect:" + adminPath + "/pay/banner/?repage";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Banner banner, @RequestParam(required=false) Boolean isRe, RedirectAttributes redirectAttributes) {
		
		bannerService.delete(banner);
		addMessage(redirectAttributes, (isRe!=null&&isRe?"发布":"删除")+"Banner信息成功");
		return "redirect:" + adminPath + "/pay/banner/?repage";
	}
	
}
